import java.io.*;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.rdf.model.impl.*;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.*;

/** 
 * @author TA "Cunxin Jia"
 * @author modified by Dr. "Wei Hu"
 */
public class soccer 
{
	public static String prefix = "http://www.semanticweb.org/ontologies/2012/6/Ontology1342505298961.owl#";
	public static Resource Player = new ResourceImpl(prefix + "Player");
	public static Resource Forward = new ResourceImpl(prefix + "Forward");
	public static Resource Real_Madrid = new ResourceImpl(prefix + "Real_Madrid");
	
	
	private OntModel model;
	
	public static void main(String[] args) throws FileNotFoundException 
	{
		soccer sample = new soccer();
		
		String path = "file:D:/eclipse/workspace/soccer/soccer.owl";
		sample.init(path);
		
		//sample.printClasses();
		//sample.printObjectProperties();
		//sample.printDatatypeProperties();
		//sample.printIndividuals();
		sample.printRDFTriples();	
		
		//sample.printPropertiesByDomain(Player);
		//sample.printSuperClass(Player); 
		
		//sample.addInstance();
		//sample.writeToFile("D:/eclipse/workspace/soccer/new_soccer.owl");
	}
	
	public void init(String path) 
	{
		OntDocumentManager mgr = new OntDocumentManager();
		mgr.setProcessImports(false);
		// To support RDFS inference, change to OWL_DL_MEM_RDFS_INF
		OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_DL_MEM); 
		spec.setDocumentManager(mgr);
		model = ModelFactory.createOntologyModel(spec, null);
		model.read(path);
	}
	
	public void printClasses() 
	{
		 System.out.println("------printClasses------");
         Iterator iter=model.listClasses();
         while (iter.hasNext())
         {
        	 Object ob=iter.next();
        	 System.out.println(ob.toString());
         } 
         System.out.println("------printClasses------");
	}
	
	public void printObjectProperties() 
	{
		 System.out.println("------printObjectProperties------");
		 Iterator iter=model.listObjectProperties();
		 while (iter.hasNext())
		 {
			 Object ob=iter.next();
			 System.out.println(ob.toString());
		 }
		 System.out.println("------printObjectProperties------");
	}
	
	public void printDatatypeProperties() 
	{
		 System.out.println("------printDatatypeProperties------");
		 Iterator iter=model.listDatatypeProperties();
		 while (iter.hasNext())
		 {
			 Object ob=iter.next();
			 System.out.println(ob.toString());
		 }
		 System.out.println("------printDatatypeProperties------");
	}
	
	public void printIndividuals() 
	{
		System.out.println("------printIndividuals------");
		Iterator iter=model.listIndividuals();
		while (iter.hasNext())
		{
			Object ob=iter.next();
			System.out.println(ob.toString());
		}
		System.out.println("------printIndividuals------");
	}
	
	public void printRDFTriples() 
	{
		System.out.println("------printRDFTriples------");
		StmtIterator iter=model.listStatements();
		while(iter.hasNext())
		{
			Statement stmt=iter.nextStatement();
			Resource  subject   = stmt.getSubject();     
		    Property  predicate = stmt.getPredicate();   
		    RDFNode   object    = stmt.getObject();    
		    System.out.print(subject.toString());
		    System.out.print(" " + predicate.toString() + " ");
		    if (object instanceof Resource) {
		       System.out.print(object.toString());
		    } else {
		        // object is a literal
		        System.out.print(" \"" + object.toString() + "\"");
		    }

		    System.out.println(" .");
		}
		System.out.println("------printRDFTriples------");
	}
	
	public void printPropertiesByDomain(Resource domainClass) 
	{
		System.out.println("------printPropertiesByDomain------");
		ExtendedIterator<FunctionalProperty> iter=model.listFunctionalProperties();
		while(iter.hasNext())
		{
			FunctionalProperty ob=iter.next();
			if (ob.getDomain().equals(domainClass))
				System.out.println(ob.toString());			
		}
		System.out.println("------printPropertiesByDomain------");
	}
	
	public void printSuperClass(Resource aClass) 
	{
		System.out.println("------printSuperClass------");
		ExtendedIterator<OntClass> iter=model.listClasses();
		while(iter.hasNext())
		{
			OntClass ob=iter.next();
			if(ob.hasSuperClass() && ob.equals(aClass))
			{
				ExtendedIterator<OntClass> iter2=ob.listSuperClasses();
				while(iter2.hasNext())
				{
					OntClass oc=iter2.next();
					System.out.println(oc.toString());
				}
			}
		}
		System.out.println("------printSuperClass------");
	}
	
	public void addInstance() 
	{
		OntClass player=model.createClass(prefix+"Player");
	    //Resource Cristiano_Ronaldo = model.createResource(prefix + "Cristiano_Ronaldo");
		Individual Cristiano_Ronaldo= player.createIndividual(prefix+"Cristiano_Ronaldo");
		Property plays_position=model.getProperty(prefix+"plays_position");
		Property isemployeeof=model.getProperty(prefix+"isemployeeof");
		Property nationality=model.getProperty(prefix+"nationality");
		Property age=model.createProperty(prefix+"age");
		Cristiano_Ronaldo.addProperty(plays_position, Forward);
		Cristiano_Ronaldo.addProperty(isemployeeof, Real_Madrid);
		model.addLiteral(Cristiano_Ronaldo, age, 27);
		Cristiano_Ronaldo.addProperty(nationality,"Portugal");
	}
	
	public void writeToFile(String path) throws FileNotFoundException 
	{
		FileOutputStream fout=new FileOutputStream(path);
		model.write(fout,"RDF/XML-ABBREV");
		// Use "RDF/XML-ABBREV" for model.write()
	}
}
